// timetable/forLeave/forLeave.js
var app=getApp();
var util=require("../../utils/util");
var table=require("../../api/table");

Page({
  /*页面的初始数据*/
  data: {
    value: '',
    num: 0,
    confirm: true,
    token: ''
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that=this;
    that.setData({did:options.did,cid:options.cid,name:options.name,token:wx.getStorageSync('token')})
  },
  input_value:function(e){
    var that = this;
    var val = e.detail.value, num = e.detail.cursor;
    that.setData({value: val, num: num})
  },
  // 请假
  confirm_leave:function(){
    var that = this,val=that.data.value,testSpace = /^[ ]*$/g,confirm=that.data.confirm;
    if(testSpace.test(val)||!val){
      util.page_show_toast('请填写请假理由吧~',2000);
      return false;
    }
    if(confirm){
      //
      that.user_set_leave(val)
      that.setData({confirm:false})
    }
  },
  // 请假请求
  user_set_leave:function(val){
    var that=this,cid=that.data.cid,name=that.data.name,did=that.data.did,token=that.data.token;
    var params={
      "content": util.removeAllSpace(val),
      "studentId": cid,
      "studentName": name,
      "scheduleDetailId": did
    }
    table.user_set_leave(params,token).then(res=>{
      if(res.data.code==200){
        app.globalData.set_leave=true
        util.page_show_toast('提交成功',1500);
        setTimeout(function(){
          wx.navigateBack({
            delta: 1
          });
          that.setData({confirm:true})
        },1500)
      }
    })
  },
})